/* eslint-disable */
import { hiprint } from "../hiprint.bundle.js";

export const defaultElementTypeProvider = (function () {
	return function (options) {
		var addElementTypes = function (context) {
			context.addPrintElementTypes("defaultModule", [
				new hiprint.PrintElementTypeGroup("常规", [
					{
						tid: "defaultModule.text",
						title: "文本",
						data: "",
						type: "text",
					},
					{
						tid: "defaultModule.image",
						title: "图片",
						data: "@/assets/images/holder.jpg",
						type: "image"
					},
					{
						tid: "defaultModule.longText",
						title: "长文",
						data: "155123456789",
						type: "longText"
					},
					{
						tid: 'defaultModule.table', field: 'table', text: '表格',
						type: 'table',
						editable: true,
						columnDisplayEditable: true,//列显示是否能编辑
						columnDisplayIndexEditable: true,//列顺序显示是否能编辑
						columnTitleEditable: true,//列标题是否能编辑
						columnResizable: true, //列宽是否能调整
						columnAlignEditable: true,//列对齐是否调整
						columns: [
							[
								{
									title: '费用项目', align: 'center', field: 'name',
								},
								{
									title: '开始时间', align: 'center', field: 'billStartDate',
									formatter: (val) => val && val.split(' ')[0]
								},
								{
									title: '结束时间', align: 'center', field: 'billEndDate',
									formatter: (val) => val && val.split(' ')[0]
								},
								{
									title: '起数', align: 'center', field: 'dosageStartNum',
									formatter: (val) => val || '-'
								},
								{
									title: '止数', align: 'center', field: 'dosageEndtNum',
									formatter: (val) => val || '-'
								},
								{
									title: '单价', align: 'center', field: 'price',
									formatter: (_, { price, meteringUnitName }) => meteringUnitName ? `${price < 0 ? '阶梯价' : price}(${meteringUnitName})` : ''
								},
								{ title: '数量', align: 'center', field: 'numbers', },
								{ title: '金额', align: 'center', field: 'billAmount', },
								{ title: '滞纳金', align: 'center', field: 'defalutAmount', },
								{ title: '备注', align: 'center', field: 'remark', },
							]
						],
						footerFormatter: (options, rows, data, currentPageGridRowsData) => {
							console.log(options, rows, data, currentPageGridRowsData)
							function ToString(n) {

								if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n)) {
									return "数据非法";  //判断数据是否大于0
								}

								var unit = "千百拾亿千百拾万千百拾元角分", str = "";
								n += "00";

								var indexpoint = n.indexOf('.');  // 如果是小数，截取小数点前面的位数

								if (indexpoint >= 0) {

									n = n.substring(0, indexpoint) + n.substr(indexpoint + 1, 2);   // 若为小数，截取需要使用的unit单位
								}

								unit = unit.substr(unit.length - n.length);  // 若为整数，截取需要使用的unit单位
								for (var i = 0; i < n.length; i++) {
									str += "零壹贰叁肆伍陆柒捌玖".charAt(n.charAt(i)) + unit.charAt(i);  //遍历转化为大写的数字
								}

								return str.replace(/零(千|百|拾|角)/g, "零").replace(/(零)+/g, "零").replace(/零(万|亿|元)/g, "$1").replace(/(亿)万|壹(拾)/g, "$1$2").replace(/^元零?|零分/g, "").replace(/元$/g, "元整"); // 替换掉数字里面的零字符，得到结果

							}
							let money = rows.reduce((pre, cur) => pre + (Number(cur.billAmount) || 0), 0)
							const capitalMoney = money && ToString(money)
							return `
							      <tr>
							      <td style="text-align: center;">合计大写</td>
							      <td style="text-align: center;" colSpan="4">${capitalMoney || ''}</td>
							      <td style="text-align: center;" >小写</td>
							      <td style="text-align: center;" colSpan="4">${money || ''}</td>
							      </tr>
							      `
						}
					},
					{
						tid: "defaultModule.tableCustom",
						title: "自定义表格",
						type: "tableCustom",
					},
					{
						tid: "defaultModule.html",
						title: "html",
						formatter: function (data, options) {
							return $(
								'div style="height:50pt;width:50pt;background:red;border-radius: 50%;"></div>'
							);
						},
						type: "html"
					},
					{
						tid: "defaultModule.customText",
						title: "自定义文本",
						customText: "自定义文本",
						custom: true,
						type: "text"
					}
				]),
				new hiprint.PrintElementTypeGroup("辅助", [
					{
						tid: "defaultModule.hline",
						title: "横线",
						type: "hline"
					},
					{
						tid: "defaultModule.vline",
						title: "竖线",
						type: "vline"
					},
					{
						tid: "defaultModule.rect",
						title: "矩形",
						type: "rect"
					},
					{
						tid: "defaultModule.oval",
						title: "椭圆",
						type: "oval"
					}
				]),
			]);
		};

		return {
			addElementTypes: addElementTypes
		};
	};
})();
